package cn.iocoder.yudao.module.pay.bill.mq;

import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.module.api.member.agency.message.AgencyOrderPaidMessage;
import cn.iocoder.yudao.module.api.member.agency.vo.AgencyOrderVO;
import cn.iocoder.yudao.module.api.member.alliance.message.CloudAllianceOrderPaidMessage;
import cn.iocoder.yudao.module.api.member.alliance.vo.CloudAllianceOrderVO;
import cn.iocoder.yudao.module.api.member.bonus.message.BonusOrderPaidMessage;
import cn.iocoder.yudao.module.api.member.bonus.vo.BonusOrderVO;
import cn.iocoder.yudao.module.api.member.vip.message.VipOrderPaidMessage;
import cn.iocoder.yudao.module.api.member.vip.vo.VipOrderVO;
import cn.iocoder.yudao.module.api.pay.bill.enums.BillCategoryEnum;
import cn.iocoder.yudao.module.api.pay.bill.enums.BillTypeEnum;
import cn.iocoder.yudao.module.api.promotion.redpacket.message.RedPacketGrantMessage;
import cn.iocoder.yudao.module.api.promotion.redpacket.message.RedPacketUsageMessage;
import cn.iocoder.yudao.module.api.trade.scan.dto.ScanOrderDTO;
import cn.iocoder.yudao.module.api.trade.scan.message.ScanOrderPaidMessage;
import cn.iocoder.yudao.module.pay.bill.service.PayBillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  这里是文件说明
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/05/23
 */
@Component
@Slf4j
public class BillConsumer {

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(ScanOrderPaidMessage message) {
        ScanOrderDTO order = message.getOrder();
        log.info("[pay/bill][onMessage][线下扫码订单支付完成事件({})]", order.getTradeNo());
        SpringUtil.getBean(PayBillService.class).expendAsync(
                order.getUserId(), BillCategoryEnum.MONEY, BillTypeEnum.PAY_OFFLINE_SCAN,
                -1d * order.getPrice(), order.getNote(),
                order.getTradeNo(), "{\"shopId\":\"%s\"}".formatted(order.getShopId())
        );
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(VipOrderPaidMessage message) {
        VipOrderVO order = message.getOrder();
        log.info("[pay/bill][onMessage][会员购买事件({})]", order.getTradeNo());
        SpringUtil.getBean(PayBillService.class).expendAsync(
                order.getUserId(), BillCategoryEnum.MONEY, BillTypeEnum.PAY_VIP,
                -1d * order.getPrice(), order.getNote(),
                order.getTradeNo(), "{\"cardId\":\"%s\"}".formatted(order.getCardId())
        );
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(AgencyOrderPaidMessage message) {
        AgencyOrderVO order = message.getOrder();
        log.info("[pay/bill][onMessage][招商人员缴费完成事件({})]", order.getTradeNo());
        SpringUtil.getBean(PayBillService.class).expendAsync(
                order.getUserId(), BillCategoryEnum.MONEY, BillTypeEnum.PAY_AGENCY,
                -1d * order.getPrice(), order.getNote(),
                order.getTradeNo(), "{\"packageId\":\"%s\"}".formatted(order.getPackageId())
        );
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(CloudAllianceOrderPaidMessage message) {
        CloudAllianceOrderVO order = message.getOrder();
        log.info("[pay/bill][onMessage][云盟商缴费完成事件({})]", order.getTradeNo());
        SpringUtil.getBean(PayBillService.class).expendAsync(
                order.getUserId(), BillCategoryEnum.MONEY, BillTypeEnum.PAY_ALLIANCE,
                -1d * order.getPrice(), order.getNote(),
                order.getTradeNo(), "{\"packageId\":\"%s\"}".formatted(order.getPackageId())
        );
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(BonusOrderPaidMessage message) {
        BonusOrderVO order = message.getOrder();
        log.info("[pay/bill][onMessage][分红权限缴费完成事件({})]", order.getTradeNo());
        SpringUtil.getBean(PayBillService.class).expendAsync(
                order.getUserId(), BillCategoryEnum.MONEY, BillTypeEnum.PAY_BONUS_BENEFIT,
                -1d * order.getPrice(), order.getNote(),
                order.getTradeNo(), ""
        );
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(RedPacketGrantMessage message) {
        log.info("[pay/bill][onMessage][商家红包发放事件({})]", message);
        SpringUtil.getBean(PayBillService.class).incomeAsync(
                message.getUserId(), BillCategoryEnum.RED_PACKET, BillTypeEnum.RED_PACKET_GRANTED,
                1d * message.getAmount(), "商家发放红包",
                String.valueOf(message.getRelatedId()), "{\"shopId\":\"%s\"}".formatted(message.getShopId())
        );
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(RedPacketUsageMessage message) {
        log.info("[pay/bill][onMessage][商家红包使用事件({})]", message);
        SpringUtil.getBean(PayBillService.class).expendAsync(
                message.getUserId(), BillCategoryEnum.RED_PACKET, BillTypeEnum.RED_PACKET_USED,
                -1d * message.getAmount(), "红包抵扣",
                message.getTradeNo(), "{\"shopId\":\"%s\"}".formatted(message.getShopId())
        );
    }

}
